package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderParam;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.sun.javafx.collections.MappingChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/submit")
    public Result submit(@RequestBody OrderParam param) {
        log.info("[提交预约信息]");
        String telephone = param.getTelephone();
        String validateCode = param.getValidateCode();
        String codeFromRedis = (String) redisTemplate.opsForValue().get("member_loginPhone:" + telephone);
        if (codeFromRedis == null) {
            return new Result(false, "验证码过期或未点击发送验证码");
        }
        if (!validateCode.equals(codeFromRedis)) {
            return new Result(false, "验证码错误");
        }
        //添加数据
        orderService.order(param);
        //需要一个返回值orderid 创建方法
        Order orderId = orderService.findOrderId(param);

        return new Result(true, MessageConst.UPLOAD_SUCCESS, orderId);

    }


    //根据预约人id，查询预约信息
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        log.info("[根据预约人id，查询预约信息],data:{}", id);
        //将结果封装到返回
        OrderParam param = orderService.findById(id);
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS, param);
    }
}
